<extend name="Public:base" />
<block name="content">
<div class="page-header">
		<h1>
			{$breadcrumb2}
			<small>
				<i class="icon-double-angle-right"></i>
				{$crumbs}
			</small>
			
			<button name="send" form="form-goods" type="submit" style="float:right;"  class="btn btn-sm btn-primary">提交</button>
			
		</h1>
	</div>
	<div class="row">
	<div class="col-xs-12">	
		<div class="panel-body">
        <form action="<?php echo $action; ?>" method="post" enctype="multipart/form-data" id="form-goods" class="form-horizontal">
        
        <notempty name="Think.get.id">
			<input name="goods_id" type="hidden" value="{$Think.get.id}" />
		</notempty>
          <ul class="nav nav-tabs">
            <li class="active"><a href="#tab-general" data-toggle="tab">常规项</a></li>
            <li><a href="#tab-data" data-toggle="tab">基本信息</a></li>
            <li><a href="#tab-links" data-toggle="tab">关联</a></li>   
            <li><a href="#tab-discount" data-toggle="tab">折扣</a></li>        
            <li><a href="#tab-image" data-toggle="tab">图片参数</a></li>            
          </ul>
          <div class="tab-content">
          	<!-- 常规 START -->
	          	<div class="tab-pane active" id="tab-general">
	          		
	          		<div class="form-group required">
						<label class="col-sm-2 control-label" for="input-name2">商品名称：</label>
						<div class="col-sm-10">
							<input id="input-name2" class="form-control" type="text" placeholder="商品名称：" value="{$description.name|default=''}" name="goods_description[name]">
						</div>
					</div>
					
					<div class="form-group">
	                    <label class="col-sm-2 control-label" for="input-description">商品简介：</label>
	                    <div class="col-sm-10">
	                      <textarea name="goods_description[summary]" class="form-control" rows="5">{$description.summary|default=''}</textarea>	
	                    </div>
                  	</div>
					
					<div class="form-group">
	                    <label class="col-sm-2 control-label" for="input-description">商品详情：</label>
	                    <div class="col-sm-10">
	                      <textarea name="goods_description[description]" id="description">{$description.description|default=''}</textarea>	
	                    </div>
                  	</div>
		
					
					<div class="form-group">
						<label class="col-sm-2 control-label" for="input-meta-description2">Meta标签描述：</label>
						<div class="col-sm-10">
							<textarea id="input-meta-description2" class="form-control" placeholder="Meta标签描述：" rows="5" name="goods_description[meta_description]">{$description.meta_description|default=''}</textarea>
						</div>
					</div>
					<div class="form-group">
						<label class="col-sm-2 control-label" for="input-meta-keyword2">Meta标签关键词：</label>
						<div class="col-sm-10">
							<textarea id="input-meta-keyword2" class="form-control" placeholder="Meta标签关键词：" rows="5" name="goods_description[meta_keyword]">{$description.meta_keyword|default=''}</textarea>
						</div>
					</div>
					
					<div class="form-group">
						<label class="col-sm-2 control-label" for="input-tag2">
							<span title="" data-toggle="tooltip" data-original-title="使用逗号分开">商品标签：</span>
						</label>
						<div class="col-sm-10">
							<input id="input-tag2" class="form-control" type="text" placeholder="商品标签：" value="{$description.tag|default=''}" name="goods_description[tag]">
						</div>
					</div>
					
	          	</div>
	          	<!-- 常规 END -->
	          	<!-- 基本信息 START -->
	          	<div class="tab-pane" id="tab-data">
	          		
	          		<div class="form-group required">
		                <label class="col-sm-2 control-label" for="input-image">
		                <span title="" data-toggle="tooltip" data-original-title="上传800x800的图片">商品图片：</span>
		                </label>
		                
		                <div class="col-sm-10" id="thumb">
		                  <a href="#" data-toggle="image" class="img-thumbnail">
		                  	<img osctype="image" <if condition="$goods['image']"> 
								src="__ROOT__{$goods.thumb_image}" 
								<else /> 
								src="__ROOT__/Common/image/no_image_100x100.jpg" 
								</if>  />
								</a>
		                  <input osctype="image_input" type="hidden" name="image" value="{$goods.image|default=''}" id="input-image" />
		            </div>
		            
		            </div>   
		            
		            <div class="form-group required">
						<label class="col-sm-2 control-label" for="input-meta-title2">商品型号：</label>
						<div class="col-sm-10">
							<input id="input-meta-title2" class="form-control" type="text" placeholder="商品型号" value="{$goods.model|default=''}" name="model">
						</div>
					</div>
					
					<div class="form-group required">
						<label class="col-sm-2 control-label" for="input-meta-title2">库存单位：</label>
						<div class="col-sm-10">
							<input id="input-meta-title2" class="form-control" type="text" placeholder="库存单位" value="{$goods.sku|default=''}" name="sku">
						</div>
					</div>
					
					<div class="form-group required">
						<label class="col-sm-2 control-label" for="input-meta-title2">生产地：</label>
						<div class="col-sm-10">
							<input id="input-meta-title2" class="form-control" type="text" placeholder="商品位置" value="{$goods.location|default=''}" name="location">
						</div>
					</div>
					
					<div class="form-group required">
						<label class="col-sm-2 control-label" for="input-meta-title2">销售价格：</label>
						<div class="col-sm-10">
							<input id="input-meta-title2" class="form-control" type="text" placeholder="销售价格" value="{$goods.price|default=''}" name="price">
						</div>
					</div>
					
					<div class="form-group required">
						<label class="col-sm-2 control-label" for="input-meta-title2">商品数量：</label>
						<div class="col-sm-10">
							<input id="input-meta-title2" class="form-control" type="text" placeholder="商品数量" value="{$goods.quantity|default=''}" name="quantity">
						</div>
					</div>
					
					<div class="form-group required">
						<label class="col-sm-2 control-label" for="input-meta-title2">最小起定量：</label>
						<div class="col-sm-10">
							<input id="input-meta-title2" class="form-control" type="text" placeholder="最小起定量" value="{$goods.minimum|default=''}" name="minimum">
						</div>
					</div>
					
					<div class="form-group">
						<label class="col-sm-2 control-label" for="input-subtract">扣减库存：</label>
						
						
							<div class="col-sm-10">
								<label class="radio-inline">
								<input type="radio" checked="checked" value="1" name="subtract">是</label>
								<label class="radio-inline">
								<input type="radio" value="0" name="subtract">否</label>
							</div>
						
					</div>
		            
		            <div class="form-group">
						<label class="col-sm-2 control-label" for="input-stock-status">
						库存状况：</label>
						<div class="col-sm-10">							
							<select id="input-stock-status" class="form-control" name="stock_status_id">
								<volist name="stock_status" id="v">
								<option value="{$v.stock_status_id}" <if condition="isset($goods)&&($v.stock_status_id eq $goods['stock_status_id'])">selected="selected"</if>  >{$v.name}</option>
								</volist>
							</select>
						</div>
					</div>
					
					<div class="form-group">
						<label class="col-sm-2 control-label">需要配送：</label>
						<div class="col-sm-10">
							<label class="radio-inline">
							<input type="radio" checked="checked" value="1" name="shipping">是</label>
							<label class="radio-inline">
							<input type="radio" value="0" name="shipping">否</label>
						</div>
					</div>

					<div class="form-group">
						<label class="col-sm-2 control-label" for="input-length">包装尺寸(L x W x H)：</label>
						<div class="col-sm-10">
							<div class="row">
								<div class="col-sm-4">
									<input id="input-length" class="form-control" type="text" placeholder="长度：" value="{$goods.length|default=''}" name="length">
								</div>
								<div class="col-sm-4">
									<input id="input-width" class="form-control" type="text" placeholder="宽度" value="{$goods.width|default=''}" name="width">
								</div>
								<div class="col-sm-4">
									<input id="input-height" class="form-control" type="text" placeholder="高度" value="{$goods.height|default=''}" name="height">
								</div>
							</div>
						</div>
					</div>


					<div class="form-group">
						<label class="col-sm-2 control-label" for="input-length-class">尺寸单位：</label>
						<div class="col-sm-10">
							
							<select id="input-length-class" class="form-control" name="length_class_id">
								<volist name="length_class" id="v">
									<option <if condition="isset($goods)&&($v.length_class_id eq $goods['length_class_id'])">selected="selected"</if> value="{$v.length_class_id}">{$v.title}</option>
								</volist>
							</select>
						</div>
					</div>
					
					<div class="form-group required">
						<label class="col-sm-2 control-label" for="input-meta-title2">重量：</label>
						<div class="col-sm-10">
							<input id="input-meta-title2" class="form-control" type="text" placeholder="重量" value="{$goods.weight|default=''}" name="weight">
						</div>
					</div>

					<div class="form-group">
						<label class="col-sm-2 control-label" for="input-length-class">重量单位：</label>
						<div class="col-sm-10">
							
							<select id="input-length-class" class="form-control" name="weight_class_id">
								<volist name="weight_class" id="v">
									<option <if condition="isset($goods)&&($v.weight_class_id eq $goods['weight_class_id'])">selected="selected"</if> value="{$v.weight_class_id}"  >{$v.title}</option>
								</volist>
							</select>
						</div>
					</div>
		            
		            <div class="form-group">
						<label class="col-sm-2 control-label">商品状态：</label>
						<div class="col-sm-10">
							<label class="radio-inline">
							<input type="radio" checked="checked" value="1" name="status">启用</label>
							<label class="radio-inline">
							<input type="radio" value="0" name="status">停用</label>
						</div>
					</div>
		            
		            <div class="form-group required">
						<label class="col-sm-2 control-label" for="input-meta-title2">排序：</label>
						<div class="col-sm-10">
							<input id="input-meta-title2" class="form-control" type="text" placeholder="排序" value="{$goods.sort_order|default=''}" name="sort_order">
						</div>
					</div>
		            
	          	</div>
	          	<!-- 基本信息 END -->
	          	<!--折扣 START -->
	          	<div class="tab-pane" id="tab-discount">
	          		<div class="table-responsive">
	                	<table id="discount" class="table table-striped table-bordered table-hover">
							<thead>
								<tr>
								  <td class="text-left">数量</td>
								  <td class="text-right">价格</td>
								  <td></td>
								</tr>
							</thead>
							<tbody>
							<?php $discount_row = 0; ?>
		                     <?php if(isset($goods_discount)){ ?>
			                    <?php foreach ($goods_discount as $discount) { ?>
				                    <tr id="discount-row<?php echo $discount_row; ?>">
				                      <td class="text-left"><input type="text" name="goods_discount[<?php echo $discount_row; ?>][quantity]" value="<?php echo $discount['quantity']; ?>" class="form-control" /></td>
				                      <td class="text-left"><input type="text" name="goods_discount[<?php echo $discount_row; ?>][price]" value="<?php echo $discount['price']; ?>" class="form-control" /></td>
				                      <td class="text-left"><button type="button" onclick="$('#discount-row<?php echo $discount_row; ?>').remove();" data-toggle="tooltip" class="btn btn-danger"><i class="icon-trash"></i></button></td>
				                    </tr>
			                    <?php $discount_row++; ?>
			                    <?php } ?>
			                 <?php } ?>
							</tbody>
						</table>
		                <div><a  onclick="addDiscount();" class="add_image btn btn-primary ">添加折扣</a></div>
                  </div>
	          		
	          		
	          	</div>
	          	<!--折扣 END -->
	          	<!-- 图片 START -->
	          	<div class="tab-pane" id="tab-image">
	          		<div class="table-responsive">
	                	<table id="images" class="table table-striped table-bordered table-hover">
		                  <thead>
		                    <tr>
		                      <td class="text-left">商品图片</td>
		                      <td class="text-right">选项排序</td>
		                      <td></td>
		                    </tr>
		                  </thead>
		                	 <tbody>
                     <?php $image_row = 0; ?>
                     <?php if(isset($goods_images)){ ?>
	                    <?php foreach ($goods_images as $goods_image) { ?>
		                    <tr id="image-row<?php echo $image_row; ?>">
		                      <td class="text-left"><a href="" id="thumb-image<?php echo $image_row; ?>" num="<?php echo $image_row; ?>" data-toggle="image" class="img-thumbnail"><img osctype="gallery_image<?php echo $image_row; ?>" src="<?php echo $goods_image['thumb']; ?>" alt="" title="" /></a><input osctype="gallery_image_input<?php echo $image_row; ?>" type="hidden" name="goods_image[<?php echo $image_row; ?>][image]" value="<?php echo $goods_image['image']; ?>" id="input-image<?php echo $image_row; ?>" /></td>
		                      <td class="text-right"><input type="text" name="goods_image[<?php echo $image_row; ?>][sort_order]" value="<?php echo $goods_image['sort_order']; ?>" class="form-control" /></td>
		                      <td class="text-left"><button type="button" onclick="$('#image-row<?php echo $image_row; ?>').remove();" data-toggle="tooltip" class="btn btn-danger"><i class="icon-trash"></i></button></td>
		                    </tr>
	                    <?php $image_row++; ?>
	                    <?php } ?>
	                 <?php } ?>
                  </tbody>
		          </table>
		                <div><a  onclick="addImage();" class="add_image btn btn-primary ">添加图片</a></div>
                  </div>
	          	</div>
	          	<!-- 图片 END -->
	          	
	          	<!-- 关联 START -->
	          	<div class="tab-pane" id="tab-links">
	          		<div class="form-group">
	                <label class="col-sm-2 control-label" for="input-category"><span data-toggle="tooltip" title="(自动完成)">商品分类</span></label>
	                <div class="col-sm-10">
	                  <input type="text" name="category" value="" placeholder="商品分类" id="input-category" class="form-control" />
	                  <div id="goods-category" class="well well-sm" style="height: 150px; overflow: auto;">
	                  	<?php if(isset($goods_categories)){ ?>
		                    <?php foreach ($goods_categories as $goods_category) { ?>
			                    <div id="goods-category<?php echo $goods_category['category_id']; ?>"><i class="icon-ban-circle"></i> <?php echo $goods_category['name']; ?>
			                      <input type="hidden" name="goods_category[]" value="<?php echo $goods_category['category_id']; ?>" />
			                    </div>
		                    <?php } ?>
	                    <?php } ?>
	                  </div>
	                </div>
	              </div>
	              

	              
	          	</div>
	          	<!-- 关联 END -->
	       
        
          </div>
        </form>
	</div>
	</div>
</div>
</block>
<block name="javascript">
<style>
.table thead > tr > td, .table tbody > tr > td {
    vertical-align: middle;
}	
.table thead td span[data-toggle="tooltip"]:after, label.control-label span:after {
	font-family: FontAwesome;
	color: #1E91CF;
	content: "\f059";
	margin-left: 4px;
}
</style>	

<script src="__PUBLIC__/ckeditor/ckeditor.js"></script> 
<script src="__PUBLIC__/fileupload/jquery.ui.widget.js"></script>
<script src="__PUBLIC__/fileupload/jquery.fileupload.js"></script>
<script>
	CKEDITOR.replace('description', {
		filebrowserImageUploadUrl: '{:U("Image/ckupload")}',	
		toolbar: [
					[ 'Source', 'Bold', 'Italic', 'Underline', 'Strike', 'Subscript', 'Superscript', '-', 'RemoveFormat' ],
					[ 'FontSize', 'TextColor', 'BGColor' ],
					[ 'JustifyLeft', 'JustifyCenter', 'JustifyRight', 'JustifyBlock','Image','Format']
				]	
	});
	
	
$('input[name=\'category\']').autocomplete({
	'source': function(request, response) {
		$.ajax({
			url: '{:U("GoodsCategory/autocomplete")}' +'/filter_name/'+  encodeURIComponent(request),
			dataType: 'json',			
			success: function(json) {
				response($.map(json, function(item) {
					return {
						label: item['name'],
						value: item['category_id']
					}
				}));
			}
		});
	},
	'select': function(item) {
		$('input[name=\'category\']').val('');
		
		$('#goods-category' + item['value']).remove();
		
		$('#goods-category').append('<div id="goods-category' + item['value'] + '"><i class="icon-ban-circle"></i> ' + item['label'] + '<input type="hidden" name="goods_category[]" value="' + item['value'] + '" /></div>');	
	}
});	
$('#goods-category').delegate('.icon-ban-circle', 'click', function() {
	$(this).parent().remove();
});	
	
	var image_row = '<?php echo $image_row; ?>';

	function addImage() {
		html  = '<tr id="image-row' + image_row + '">';
		html += '  <td class="text-left"><a href="#" id="thumb-image' + image_row + '"data-toggle="image" num="'+image_row+'" class="img-thumbnail"><img osctype="gallery_image'+image_row+'" src="__ROOT__/Common/image/no_image_100x100.jpg" alt="" title="" /><input osctype="gallery_image_input'+image_row+'" type="hidden" name="goods_image[' + image_row + '][image]" value="" id="input-image' + image_row + '" /></td>';
		html += '  <td class="text-right"><input type="text" name="goods_image[' + image_row + '][sort_order]" value="'+image_row+'" class="form-control" /></td>';
		html += '  <td class="text-left"><button type="button" onclick="$(\'#image-row' + image_row  + '\').remove();" data-toggle="tooltip" class="btn btn-danger"><i class="icon-trash"></i></button></td>';
		html += '</tr>';
		
		$('#images tbody').append(html);
		
		image_row++;
	}

	var discount_row ={$discount_row|default='0'};

	function addDiscount() {
		html  = '<tr id="discount-row' + discount_row + '">'; 
		html += '  <td class="text-left"><input type="text" name="goods_discount[' + discount_row + '][quantity]" value="" class="form-control" /></td>';
		html += '  <td class="text-left"><input type="text" name="goods_discount[' + discount_row + '][price]" value="" class="form-control" /></td>';
		html += '  <td class="text-left"><button type="button" onclick="$(\'#discount-row' + discount_row  + '\').remove();" data-toggle="tooltip" class="btn btn-danger"><i class="icon-trash"></i></button></td>';
		html += '</tr>';
		
		$('#discount tbody').append(html);
		
		discount_row++;
	}
	
$(function(){
	
	<present name="Think.get.id">
			Oscshop.setValue("status", {$goods.status|default=1});		
			Oscshop.setValue("subtract",{$goods.subtract|default=1});		
			Oscshop.setValue("shipping",{$goods.shipping|default=1});
	</present>
	
	
	// tooltips on hover button-upload
	$('[data-toggle=\'tooltip\']').tooltip({container: 'body', html: true});

	// Makes tooltips work on ajax generated content
	$(document).ajaxStop(function() {
		$('[data-toggle=\'tooltip\']').tooltip({container: 'body'});
	});	
	
	
	
	$(document).delegate('a[data-toggle=\'image\']', 'click', function(e) {
		e.preventDefault();
		
		var index=$(this).attr('num');
		
		//alert(index);
		
		var element = this;
		
		if(index==undefined){
			$(element).popover({
				html: true,
				placement: 'right',
				trigger: 'manual',
				content: function() {
					return '<button type="button" id="thumb-image"  class="btn btn-primary"><i class="icon-edit"></i></button> <button type="button" id="button-clear" class="btn btn-danger"><i class="icon-trash"></i></button>';
				}
			});
		}else{
			$(element).popover({
				html: true,
				placement: 'right',
				trigger: 'manual',
				content: function() {
					return '<button type="button" n="'+index+'"  class="btn btn-primary button-image"><i class="icon-edit"></i></button> <button type="button" id="button-clear" class="btn btn-danger"><i class="icon-trash"></i></button>';
				}
			});
		}
		

		
		$(element).popover('toggle');	
		
		//商品图片
		$('#thumb-image').on('click', function() {
			
			//alert('333');
			
			$('#modal-image').remove();
			
			$('#form-upload').remove();
			
			$('body').prepend('<form enctype="multipart/form-data" id="form-upload" style="display: none;"><input osctype="btn_upload_image" type="file" name="file" /></form>');
	
			$('#form-upload input[name=\'file\']').trigger('click');
			
			$(element).popover('hide');
			
			$('[osctype="btn_upload_image"]').fileupload({
				
	        	dataType: 'json',
	            url: "{:U('Image/upload_image',array('dir'=>'product'))}",
	            add: function(e, data) {
	                $parent = $('#thumb');
	                $input = $parent.find('[osctype="image_input"]');
	                $img = $parent.find('[osctype="image"]');
	                data.formData = {old_goods_image:$input.val()};
	                $img.attr('src', "__IMG__/loading.gif");
	                data.submit();
	            },
	            done: function (e,data) {
					
	            	var image=data.result;
	            	
	            	
	                $parent = $('#thumb');
	                $input = $parent.find('[osctype="image_input"]');
	                $img = $parent.find('[osctype="image"]');
	                if(image) {
	                   // $img.prev('i').hide();
	                    $img.attr('src', '__ROOT__'+image.image_thumb);
	                    $img.show();
	                    $input.val(image.image);
	                } else {
	                    alert('上传失败');
	                }
	            }
   		 });
		});

			
		//商品相册
		$('.button-image').on('click', function() {
			$('#modal-image').remove();
			
			$('#form-upload').remove();
			
			var i=$(this).attr('n');
			
			//alert(i);
			
			$('body').prepend('<form enctype="multipart/form-data" id="form-upload" style="display: none;"><input osctype="btn_upload_image" type="file" name="file" /></form>');
	
			$('#form-upload input[name=\'file\']').trigger('click');
			
			$(element).popover('hide');
			
			$('[osctype="btn_upload_image"]').fileupload({
				
	        	dataType: 'json',
	            url: "{:U('Image/upload_image',array('dir'=>'gallery'))}",
	            add: function(e, data) {
	                $parent = $('#image-row'+i);
	                $input = $parent.find('[osctype="gallery_image_input'+i+'"]');
	                $img = $parent.find('[osctype="gallery_image'+i+'"]');
	                data.formData = {old_gallery_image:$input.val()};
	                $img.attr('src', "__IMG__/loading.gif");
	                data.submit();
	            },
	            done: function (e,data) {
					
	            	var image=data.result;	            	
	            	
	                $parent = $('#image-row'+i);
	                $input = $parent.find('[osctype="gallery_image_input'+i+'"]');
	                $img = $parent.find('[osctype="gallery_image'+i+'"]');
	                if(image) {
	                   // $img.prev('i').hide();
	                    $img.attr('src', '__ROOT__'+image.image_thumb);
	                    $img.show();
	                    $input.val(image.image);
	                } else {
	                    alert('上传失败');
	                }
	            }
   		 });
			
			
			
		});
	
		$('#button-clear').on('click', function() {
			$(element).find('img').attr('src', $(element).find('img').attr('data-placeholder'));
			
			$(element).parent().find('input').attr('value', '');
	
			$(element).popover('hide');
		});
	});
});
	
</script>
</block>